package wangwenjun.phase1.close;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @author ChangLiang
 * @date 2020/5/29
 */
public class FlagThreadExitV1 {

    private static final Logger LOGGER = LoggerFactory.getLogger(FlagThreadExitV1.class);

    static class MyTask extends Thread
    {

        private volatile boolean closed = false;

        @Override
        public void run()
        {
            LOGGER.info("I will start work");

            while (!closed && !isInterrupted()){
                //LOGGER.info("i am working.");
            }
            LOGGER.info("I will be exiting.");
        }

        public void close(){
            this.closed = true;
            this.interrupt();
        }
    }

    public static void main(String[] args) throws InterruptedException
    {

        MyTask t = new MyTask();
        t.start();
        TimeUnit.SECONDS.sleep(5);
        LOGGER.info("System will be shutdown.");
        t.close();
    }
}
